package com.example.common.user.viewmodel;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;


import com.example.common.component.UserCtrlCom;
import com.example.common.control.ICallback;

import com.example.common.component.LoginCtrlCom;
import com.example.common.model.CaptchaServiceGenerateResp;


/**
 * 注册页面ViewModel
 */
public class RegisterViewModel extends ViewModel {
    private static final String TAG="RegisterViewModel";

    private MutableLiveData<Boolean> isGetCaptcha=new MutableLiveData<>();//获取验证码状态
    private MutableLiveData<Boolean> isRegister=new MutableLiveData<>();//获取注册状态
    private MutableLiveData<Boolean> isShowProgressBar=new MutableLiveData<>(); //进度条状态

    private MutableLiveData<String> remainMes=new MutableLiveData<>();//提示消息

    private String Id;//验证码的Id
    private String phoneNumber;//手机号
    private String password;//密码
    private String inviteCode;//邀请码
    private Bitmap bitmap;
    private String captcha;
    private int status;//提示消息等级

    private LoginCtrlCom loginCtrlCom;

    public RegisterViewModel(){
        //加载数据
        initData();
    }

    /**
     * 数据初始化
     */
    private void initData(){
        loginCtrlCom=new LoginCtrlCom();
        //加载验证码
        loadCaptcha();
    }


    /**
     * 点击获取验证码
     * @param view
     */
    public void onClickGetCaptcha(View view){
        //获取验证码
        loadCaptcha();
    }

    /**
     * 点击注册
     * @param view
     */
    public void onClickRegister(View view){
        //检测手机号是否为空
        if(phoneNumber==null||phoneNumber.equals("")){
            status=3;
            remainMes.setValue("手机号为空");
            return;
        }
        //检测密码是否为空
        if(password==null||password.equals("")){
            status=3;
            remainMes.setValue("密码为空");
            return;
        }
        //检测邀请码是否为空
        if(inviteCode==null||inviteCode.equals("")){
            status=3;
            remainMes.setValue("邀请码为空");
            return;
        }
        //校验验证码
        checkCaptcha();
    }

    /**
     * 获取验证码
     */
    public void loadCaptcha(){
        loginCtrlCom.midmap(new ICallback() {
            @Override
            public void onSuccess(Object response) {
                CaptchaServiceGenerateResp captcha=(CaptchaServiceGenerateResp)response;
                //加载数据
                byte[] decoded=Base64.decode(captcha.getCaptcha().split(",")[1],Base64.DEFAULT);
                bitmap=BitmapFactory.decodeByteArray(decoded,0,decoded.length);
                Id=captcha.getId();//保存Id
                isGetCaptcha.setValue(true);
            }

            @Override
            public void onFailed(String mes) {
                remainMes.setValue(mes);
            }
        });
        isGetCaptcha.setValue(false);
    }

    /**
     * 校验验证码——通过后继续继续注册操作
     */
    private void checkCaptcha(){
        isShowProgressBar.setValue(true);
        loginCtrlCom.checkCaptcha(Id, captcha, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                Log.v(TAG,"验证通过");
                register();//进行注册
                loadCaptcha();//重新加载验证码
            }

            @Override
            public void onFailed(String mes) {
                Log.v(TAG,"验证不通过");
                loadCaptcha();//重新加载验证码
                isShowProgressBar.setValue(false);
                status=3;
                remainMes.setValue("验证码错误π_π");
            }
        });
    }

    /**
     * 注册
     */
    private void register(){
        loginCtrlCom.register(phoneNumber, password, inviteCode, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                isRegister.setValue(true);
                isShowProgressBar.setValue(false);
                Log.v(TAG,"注册成功");
            }

            @Override
            public void onFailed(String msg) {
                isRegister.setValue(false);
                isShowProgressBar.setValue(false);
                status=3;
                remainMes.setValue(msg);
                Log.v(TAG,"注册失败");
            }
        });
    }


    public Bitmap getBitmap() {
        return bitmap;
    }

    public MutableLiveData<Boolean> getIsGetCaptcha() {
        return isGetCaptcha;
    }

    public String getCaptcha() {
        return captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getPassword() {
        return password;
    }

    public String getInviteCode() {
        return inviteCode;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setInviteCode(String inviteCode) {
        this.inviteCode = inviteCode;
    }

    public MutableLiveData<Boolean> getIsRegister() {
        return isRegister;
    }

    public MutableLiveData<Boolean> getIsShowProgressBar() {
        return isShowProgressBar;
    }

    public MutableLiveData<String> getRemainMes() {
        return remainMes;
    }

    public int getStatus() {
        return status;
    }
}
